F.module('lib/template', function () {
    let _getTpl = function (str) {
            let ele = document.getElementById(str);
            if (ele) {
                let html = /^(textarea|input)$/i.test(ele.nodeName) ? ele.value : ele.innerHTML;
                return _compileTpl(html);
            } else {
                return _compileTpl(str);
            }
        },
        _dealTpl = function (str) {
            let _left = '{%',
                _right = '%}';
            let test = String(str)
                .replace(/&lt;/g, '<')
                .replace(/&gt;/g, '>')
                .replace(/[\r\t\n]/g, '');

            console.log(test);
            console.log(test = test.replace(new RegExp(_left + '=(.*?)' + _right, 'g'), "',typeof($1) ==='undefined'?'':$1,'"));
            console.log(test = test.replace(new RegExp(_left, 'g'), "');"));
            console.log(test = test.replace(new RegExp(_right,'g'), "template_array.push('"));
            return test;
        },
        _compileTpl = function (str) {
            let fnBody = `
                let template_array = [];
                let fn = (function (data) {
                    let template_key = '';
                    for (key in data) {
                        template_key += ('var ' + key + '=data[\\"' + key + '\\"];');
                    }
                    eval(template_key);
                    template_array.push('"+_dealTpl(str)+"');
                    template_key = null;
                })(templateData);
                fn = null;
                return template_array.join('');
            `;
            return new Function('templateData', fnBody);

        };
    console.log(_dealTpl('<a>{%=test%}</a>'));
    return function (str, data) {
        if (data instanceof Array) {
            let html = '';
            for (let i = 0; i < data.length; i++) {
                html += _getTpl(str)(data[i]);
            }
            return html;
        } else {
            return _getTpl(str)(data);
        }
    }
});